#include "addfrienddialog.h"

#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

#include "model/datacenter.h"
#include "utils/widgetuitl.hpp"
#include "debug.h"

/* ==================================== *
 *          添加好友窗口实现              *
 * ==================================== */
AddFriendDialog::AddFriendDialog(QWidget* parent) : QDialog(parent)
{
    // 1.设置窗口基本属性
    this->setStyleSheet("QDialog {background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(500, 600);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    // 2.网格布局主界面
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(5);
    layout->setContentsMargins(20, 0, 20, 0);
    this->setLayout(layout);

    // 3.顶部搜索栏控件
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(40);
    searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    searchEdit->setPlaceholderText("按手机号/用户序号/昵称查找");
    searchEdit->setStyleSheet("QLineEdit { border: none; border-radius: 10px; background-color: rgb(240, 240, 240); padding-left: 5px;/* font-size: 16px;*/}");

    QPushButton* searchBtn = new QPushButton();
    searchBtn->setFixedSize(40, 40);
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    searchBtn->setIconSize(QSize(25, 25));
    QString style = "QPushButton { border: none; background-color: rgb(243, 243, 243); border-radius: 10px; }"
                    "QPushButton:hover { background-color: rgb(220, 220, 220); }"
                    "QPushButton:pressed {background-color: rgb(200, 200, 200);}";
    searchBtn->setStyleSheet(style);

    layout->addWidget(searchEdit, 0, 0, 1, 8);
    layout->addWidget(searchBtn, 0, 8, 1, 1);

    // 4.添加滚动区
    initResultArea(layout);

////////// 构造测试数据
#if TEST_UI
    QIcon icon(":/resource/image/girl.jpg");
    for(int i = 0; i < 30; ++i) {
        model::UserInfo* userInfo = new model::UserInfo();
        userInfo->userId = QString::number(1000 + i);
        userInfo->nickname = "小美" + QString::number(i);
        userInfo->description = "this is test";
        userInfo->avatar = icon;
        addResultItem(*userInfo);
    }
#endif

    connect(searchBtn, &QPushButton::clicked, this, &AddFriendDialog::onClickedBySearchBtn);
}

void AddFriendDialog::addResultItem(const model::UserInfo& userInfo)
{
    FriendResultItem* friendResultItem = new FriendResultItem(userInfo);
    container->layout()->addWidget(friendResultItem);
}

void AddFriendDialog::clear()
{
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(container->layout());
    for(int i = layout->count() - 1; i >= 0; --i) {
        QLayoutItem* item = layout->takeAt(i);
        if (!item || !item->widget()) {
            continue;
        }
        item->widget()->deleteLater();
    }
}

void AddFriendDialog::setSearchText(const QString& content)
{
    searchEdit->setText(content);
}

void AddFriendDialog::initResultArea(QGridLayout* layout)
{
    // 1.创建滚动区
    QScrollArea* scrollArea = utils::WidgetUtil::createQScrollArea();
    layout->addWidget(scrollArea, 1, 0, 1, 9);

    // 2.创建数据存储区
    container = new QWidget();
    container->setObjectName("container");
    container->setStyleSheet("#container { background-color: rgb(255, 255, 255); }");
    scrollArea->setWidget(container);

    // 2-0 防止container窗口的宽度被滚动条压缩
    connect(scrollArea->verticalScrollBar(), &QScrollBar::rangeChanged, container, [=]() {
        int width = scrollArea->viewport()->width();
        container->setMinimumWidth(width);  // 始终维持布局宽度
    });

    // 2-1 为数据存储区创建垂直布局器
    QVBoxLayout* containerLayout = new QVBoxLayout();
    containerLayout->setContentsMargins(0, 0, 0, 0);
    containerLayout->setSpacing(0);
    // containerLayout->setAlignment(Qt::AlignTop);
    container->setLayout(containerLayout);
}

void AddFriendDialog::onClickedBySearchBtn()
{
    const QString& text = searchEdit->text();
    if(text.isEmpty()) {
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::searchUserDone, this, &AddFriendDialog::searchUserDoneHandle);
    dataCenter->searchUserAsync(text);
}

void AddFriendDialog::searchUserDoneHandle()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QList<model::UserInfo>* searchUserResult = dataCenter->getSearchUserResult();
    if(!searchUserResult) {
        return;
    }

    // 清空原有搜索列表
    this->clear();

    for(const auto& user : *searchUserResult) {
        this->addResultItem(user);
    }
}


/* ==================================== *
 *          搜索结果控件实现              *
 * ==================================== */
FriendResultItem::FriendResultItem(const model::UserInfo& userInfo)
    : userInfo(userInfo)
{
    // 1.基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2.单条控件的网格布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0, 0, 20, 0);
    layout->setVerticalSpacing(0);
    layout->setHorizontalSpacing(10);
    this->setLayout(layout);

    // 2-1 头像布局
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50, 50);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setIcon(userInfo.avatar);

    // 2-2 昵称布局
    QLabel* nameLabel = new QLabel();
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
    // nameLabel->setContentsMargins(0, 10, 0, 0);
    nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 600;}");
    nameLabel->setText(userInfo.nickname);

    // 2-3 签名布局
    QLabel* descLabel = new QLabel();
    descLabel->setFixedHeight(35);
    descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    // descLabel->setContentsMargins(0, 0, 0, 5);
    descLabel->setText(userInfo.description);

    // 2-4 添加按钮
    addBtn = new QPushButton();
    addBtn->setFixedSize(100, 40);
    addBtn->setText("添加好友");
    addBtn->setStyleSheet("QPushButton { border: none; background-color: rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px; } "
                          "QPushButton:pressed { background-color: rgb(200, 200, 200); }");

    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(descLabel, 1, 1, 1, 1);
    layout->addWidget(addBtn,    0, 2, 2, 1);

    connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::onClickedByAddBtn);
}

void FriendResultItem::onClickedByAddBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(userInfo.userId);

    addBtn->setEnabled(false);
    addBtn->setText("已申请");
    addBtn->setStyleSheet("QPushButton { border: none; border-radius: 10px; color: white; background-color: rgb(200, 200, 200); }");
}
